package com.baimsg.microcomponents.utils

import android.appwidget.AppWidgetManager
import android.content.ComponentName
import android.content.Context
import android.widget.RemoteViews
import com.baimsg.basic.utils.names
import com.baimsg.basic.utils.userIds
import com.baimsg.microcomponents.R
import kotlin.random.Random
import com.baimsg.microcomponents.receiver.PocWidgetReceiver

/**
 * Create by Baimsg on 2023/11/18
 *
 **/
fun updateWidget(context: Context, type: Int) {
    val remoteViews = RemoteViews(context.packageName, R.layout.poc_loading_appwidget)
    val userId = userIds.keys.random()
    remoteViews.setTextViewText(R.id.tv_user_id, "$userId")
    remoteViews.setTextViewText(R.id.tv_count, "${Random.nextInt(10)}/${Random.nextInt(40)}")
    remoteViews.setTextViewText(R.id.tv_name, names.random())
    when (type) {
        1 -> {
            remoteViews.setImageViewResource(R.id.iv_status, R.drawable.status_idle)
            remoteViews.setTextViewText(R.id.tv_status, "空闲")
        }

        2 -> {
            remoteViews.setImageViewResource(R.id.iv_status, R.drawable.status_talk)
            remoteViews.setTextViewText(R.id.tv_status, "${userIds[userId]} 正在说话")
        }

        else -> {
            remoteViews.setImageViewResource(R.id.iv_status, R.drawable.status_offline)
            remoteViews.setTextViewText(R.id.tv_status, "离线")
        }
    }

    val componentName = ComponentName(context, PocWidgetReceiver::class.java)
    AppWidgetManager.getInstance(context).updateAppWidget(componentName, remoteViews)

}